<template>
  <div class="w_h_100">
    <div class="header d_f j_c_s_b">
      <div class="d_f a_i_c">
        <h4>月度报送</h4>
        <el-dropdown @command="handleCommand">
          <span class="dropdown">
            {{ month }}
            <el-icon class="">
              <ArrowDown />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item v-for="(item, index) in arr" :key="index" :command="item">{{ item }}月</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <div class="select" v-if="isCompany=='0'">
          <el-select v-model="value" clearable class=" m_l_20" placeholder="请选择担保公司名称" >
          <el-option v-for="item in options" :key="item.usci" :label="item.name" :value="item.usci" />
        </el-select>
        </div>
      </div>
      <el-button type="primary" @click="downloadOutAll">全部导出</el-button>
    </div>
    <div class="report m_t_16 ">
      <div class="d_f f_w_w">
        <div v-for="(item, index) in records" :key="index" class="box p_r">
          <h2 class="title">{{ item.name }}</h2>
          <div class="button">
            <el-button type="primary" plain :icon="Download" @click="downloadOut(item)">下载</el-button>
            <el-button type="primary" :icon="Document" @click="detail(item)">查看详情</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang='ts'>
import { indexType } from '../../../type/report'
import { defineComponent, reactive, toRefs, onBeforeMount, onMounted } from 'vue'
import { ArrowDown, Download, Document } from '@element-plus/icons-vue'
import { superviseDownload, superviseDownloadAll, superviseFilterDate } from '../../../api/report'
import { momentDate, momentStartOf, derivation, momentEndOf, downZip } from '../../../utils/date'
import { useRouter } from 'vue-router'
export default defineComponent({
  name: '',
  components: {
    ArrowDown
  },
  setup() {
    const data: indexType = reactive({
      month: '',
      records: [
      
        {
          name: '融资担保公司月度监管报表',
          id: 'MONTHLY_FINANCING_GUA_STAT',
          path: "/report/supervise/monthlySupervision",
        },
        // {
        //   name: '政府性担保公司月度表',
        //   id: 'MONTHLY_FINANCING_GUA_STAT',
        //   path: "/report/supervise/monthlySupervision",
        // },
      ],
      startTime: '',
      endTime: '',
      arr: [],
      key: '',
      value:'',
      options:[],
      isCompany:'0'
    })
    onBeforeMount(() => {
      data.isCompany=JSON.parse(<string>localStorage.getItem('userVO')).isCompany
      let num = momentDate('MM')
      for (let i = 1; i <= num; i++) {
        data.arr.push(i)

      }
      data.key = momentDate('MM')
      data.month = momentDate('YYYY年MM月')
      methods.getBoundingClientRect()
      methods.getList()
    })
    onMounted(() => {
    })
    const router = useRouter()
    const methods = {
      getList() {
        superviseFilterDate({}).then((res: any) => {
        data.options=res
        if(res.length>0&&data.isCompany=='0'){
          data.value=res[0].usci
        }
        })
      },
      //下载当前模版
      downloadOut(item: any) {
        superviseDownload({ reportSuperviseType: item.id, startTime: data.startTime, endTime: data.endTime,usci:data.value }).then((res: any) => {
          derivation(res, item.name)
        })
      },
      //下载全部模版
      downloadOutAll() {
        superviseDownloadAll({ downloadAllType: 1, startTime: data.startTime, endTime: data.endTime,usci:data.value }).then((res: any) => {
          downZip(res,)
        })
      },
      //选择月份
      handleCommand(e: number) {
        let year = momentDate('YYYY')
        let month = e < 10 ? '0' + e : e
        data.key = month
        data.month = year + '年' + month + '月'
        methods.getBoundingClientRect()
      },
      //获取开始时间跟结束事件
      getBoundingClientRect() {
        let month = data.month.replace('年', '-')
        month = month.replace('月', '-')
        data.startTime = momentStartOf(month, 'YYYY-MM-DD')
        if (data.key == momentDate('MM')) {
      
          data.endTime = momentEndOf(month, 'YYYY-MM-DD')
        } else {
          data.endTime = momentEndOf(month, 'YYYY-MM-DD')
        }
      },
      detail(item: any) {
        router.push({ path: item.path, query: { startTime: data.startTime, endTime: data.endTime, month: data.month, key: data.key, type: item.id,value:data.value } })
      }
    }
    return {
      ...toRefs(data),
      ...methods,
      Download, Document
    }
  }
})
</script>
<style scoped lang='scss'>
.header {
  width: calc(100% - 64px);
  background: #fff;
  box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.1);
  border-radius: 6px;
  height: 20px;
  padding: 24px 32px;

  .dropdown {
    color: #1878FF;
    font-size: 16px;
    font-weight: bold;
    margin-left: 24px;
  }
}

.report {
  overflow-y: auto;
  width: 100%;
  background: #fff;
  box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.1);
  border-radius: 6px;
  height: calc(100% - 104px);
  padding: 10px 0px;

  .box {
    border: 1px solid;
    border-image: linear-gradient(142deg, rgba(155, 188, 235, 0.5) 8%, rgba(102, 142, 197, 0) 68%) 1;
    background: linear-gradient(174deg, #F5F9FF 8%, #FFFFFF 60%);
    box-shadow: 5px 5px 5px -2px rgba(9, 20, 29, 0.03);
    width: 25vw;
    height: 11vw;
    border-radius: 6px;
    margin-left: 32px;
    margin-top: 20px;


    .title {
      position: absolute;
      left: 1.66vw;
      top: 3.33vw;
    }

    .button {
      position: absolute;
      right: 40px;
      bottom: 24px;
    }
  }
}
</style>
<style lang="scss">
.select{
  .el-select{
    width: 240px !important;
  }
}
</style>